﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WMS.Infrastructure.BaseRepository;

namespace Zocono.WCS.Infrastructure.Repository
{
    public class TelegramRecordInfoRepository : BaseRepository<TelegramRecordInfo>, ITelegramRecordInfoRepository
    {
        public TelegramRecordInfoRepository(SqlSugarClient context = null) : base(context)
        {

        }

        /// <summary>
        /// 获取报文记录列表
        /// </summary>
        /// <param name="exp">查询条件</param>
        /// <param name="page">分页信息</param>
        /// <returns></returns>
        public async Task<PageDto<TelegramRecordInfoDto>> GetPagedTelegramRecordInfoAsync(Expression<Func<TelegramRecordInfo, bool>> exp, PageModel page)
        {
            PageDto<TelegramRecordInfoDto> pageDto = new PageDto<TelegramRecordInfoDto>();
            RefAsync<int> totalNumber = 0;
            var result = await Context.Queryable<TelegramRecordInfo>()
                .Where(exp)
                .OrderBy(c => c.UpdateTime, OrderByType.Desc)
                .Select((a) => new TelegramRecordInfoDto
                {
                    ID = a.ID,
                    EquipmentCode = a.EquipmentCode.ToString(),
                    TelegramUniqueIDJson = a.TelegramUniqueIDJson,
                    PlcTelegramJson = a.PlcTelegramJson,
                    WcsTelegramJson = a.WcsTelegramJson,
                    CreateTime = a.CreateTime,
                    UpdateTime = a.UpdateTime
                }).ToPageListAsync(page.PageIndex, page.PageSize, totalNumber);

            pageDto.Result = result;
            pageDto.Total = totalNumber;
            return pageDto;
        }

    }
}
